<template>
  <div class="app-container dialog-w80">
    <div class="customContainer">
      <el-form :inline="true" :model="formInline" class="demo-form-inline" size="small">
        <el-form-item>
          <el-select filterable placeholder="选择门店" v-model="formInline.store_id" @change="gethouse">
            <el-option v-for="item in store_list" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-select filterable placeholder="选择调出仓" v-model="formInline.house_id" @change="houseChange">
            <el-option v-for="item in house_list" :key="item.house_id" :label="item.house_name" :value="item.house_id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-input v-model="formInline.gift_id" placeholder="请输入礼品编号/名称" @keyup.enter.native="searchClick" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <dj-button size="small" @search="searchClick">
            <template #default>搜索</template>
          </dj-button>
        </el-form-item>
        <el-form-item>
          <el-tooltip class="item" effect="dark" placement="bottom-end">
            <div slot="content" style="line-height:20px">
              参数按英文的逗号隔开时可以批量查询，但是只支持神采编号精确匹配,最大支持100个，
              <br />参考参数：0201080156,0201090112,0201080111
            </div>
            <i class="el-icon-question"></i>
          </el-tooltip>
        </el-form-item>
        <el-form-item class="pull-right">
          <el-button size="small" type="primary" @click="addCanalClick()">新增店内仓</el-button>
        </el-form-item>
      </el-form>
      <transition name="fade" mode="out-in">
        <!-- 列表 -->
        <el-table highlight-current-row :data="list" border @sort-change="sortChange" size="small" v-loading.body="listLoading" :key="formInline.house_id">
          <el-table-column align="center" label="礼品图片" width="80">
            <template slot-scope="scope">
              <img :src="scope.row.thumb" width="60" height="60" alt />
            </template>
          </el-table-column>
          <el-table-column align="center" label="礼品名称" prop="name"></el-table-column>
          <el-table-column align="center" label="新礼品编号" prop="gift_id"></el-table-column>
          <el-table-column align="center" label="神采礼品编号" sortable>
            <template slot-scope="scope">{{scope.row.no_obsolete}}</template>
          </el-table-column>
          <el-table-column align="center" label="箱规" prop="box_num"></el-table-column>
          <el-table-column align="center" label="类别" prop="type_name"></el-table-column>
          <el-table-column align="center" label="成本价" prop="money_cost"></el-table-column>
          <el-table-column align="center" label="库存量" sortable>
            <template slot-scope="scope">{{scope.row.num}}</template>
          </el-table-column>
          <el-table-column align="center" label="前台销售">
            <template slot-scope="scope">{{scope.row.is_store_on_sale==1?'支持':'-'}}</template>
          </el-table-column>
          <el-table-column align="center" label="彩票兑换">
            <template slot-scope="scope">{{scope.row.is_ticket_convertible==1?'支持':'-'}}</template>
          </el-table-column>
          <el-table-column align="center" label="积分兑换">
            <template slot-scope="scope">{{scope.row.is_integral_convertible==1?'支持':'-'}}</template>
          </el-table-column>
          <!-- 列表的操作按钮 -->
          <el-table-column align="center" label="加入调拨">
            <template slot-scope="scope">
              <el-button style="font-size:28px" :disabled="scope.row.in_car==1" type="text"
                @click="addcar(scope.row.id,scope.row.box_num,formInline.store_id,formInline.house_id,scope.$index,scope.row.num)">
                <i class="el-icon-circle-plus"></i>
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </transition>
      <!-- 礼品调拨审单弹框 -->
      <el-dialog :close-on-click-modal="false" class="dialog_table" title="礼品调拨审单" :visible.sync="show_carList" @close="car_close">
        <div class="customHeader">
          <el-select placeholder="选择调入仓" filterable v-model="houseVal" @change="houseValChange" ref="houseSelect" @focus="isBlur">
            <el-option v-for="item in houseList" :disabled="isDisable" :key="item.house_id" :label="item.house_name" :value="item.house_id"></el-option>
          </el-select>
          <span v-if="WarnisBlur" style="color:red;font-size:12px;margin-left:20px;">需移除掉已上传破损信息的礼品，才可切换调入仓</span>
        </div>
        <el-table highlight-current-row :data="carList" v-loading.body="listLoading" border size="small">
          <el-table-column align="center" label="图片" width="70">
            <template slot-scope="scope">
              <img :src="scope.row.thumb" width="50" height="50" />
            </template>
          </el-table-column>
          <el-table-column align="center" label="名称" prop="name"></el-table-column>
          <el-table-column align="center" label="礼品编号" prop="gift_id"></el-table-column>
          <el-table-column align="center" label="箱规" prop="box_num"></el-table-column>
          <el-table-column align="center" label="库存量" prop="num"></el-table-column>
          <el-table-column align="center" label="调拨数量(个)">
            <template slot-scope="scope">
              <el-input onkeypress="return event.which>=48&&event.which<=57" type="number" onmousewheel="return false;" v-model="scope.row.gift_num"
                placeholder="数量" @input="count"></el-input>
            </template>
          </el-table-column>
          <el-table-column align="center" label="成本价">
            <template slot-scope="scope">
              <!-- 保留小数 -->
              {{scope.row.money_cost|keepTwoNum}}
            </template>
          </el-table-column>
          <el-table-column align="center" label="金额总计">
            <template slot-scope="scope">
              <!-- 保留小数 -->
              {{scope.row.money_cost*scope.row.gift_num|keepTwoNum}}
            </template>
          </el-table-column>
          <el-table-column align="center" label="入库时间" prop="in_time" v-if="is_store_main=='10'"></el-table-column>
          <el-table-column align="center" label="破损图片" v-if="is_store_main=='10'">
            <template slot-scope="scope">
              <img :src="scope.row.break_img.thumb" width="50" height="50" />
            </template>
          </el-table-column>
          <el-table-column align="center" label="破损状况及原因" prop="break_reason" v-if="is_store_main=='10'">
            <template slot-scope="scope">
              <p style=" overflow : hidden;text-overflow: ellipsis;display: -webkit-box;-webkit-line-clamp:3;-webkit-box-orient: vertical;">
                {{scope.row.break_reason}}</p>
            </template>
          </el-table-column>
          <el-table-column align="center" label="操作" width="130">
            <template slot-scope="scope">
              <el-button v-if="is_store_main=='10'" @click="uploading(scope.row,formInline.store_id)" type="primary" icon="el-icon-upload2" size="mini">
              </el-button>
              <el-button @click="delClick(scope.row.gift_id,formInline.store_id,formInline.house_id,scope.$index,scope.row.in_time)" type="danger"
                icon="el-icon-delete" size="mini"></el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="bottom_box">
          <el-button type="text" size="small" @click="delCarListClick(formInline.store_id,formInline.house_id)">清空调拨单</el-button>
          <el-button :loading="btnLoding" class="pull-right" size="small" type="primary" @click="saveClick()">确认调拨</el-button>
          <p class="pull-right" style="line-height:0">礼品种类:{{car_gift_num}} 调拨单礼品数:{{totalOrder}} 合计:{{totalPrice|keepTwoNum}}</p>
        </div>
      </el-dialog>
      <!-- 新增渠道弹框 -->
      <el-dialog :close-on-click-modal="false" width="300px" title="新增店内仓" :visible.sync="addCanalDialog" append-to-body>
        <el-form :model="addCanalForm" :inline="true" class="demo-form-inline">
          <el-form-item label="店内仓">
            <el-select placeholder="店内仓" v-model="addCanalForm.is_gift_machine" style="width:246px">
              <el-option v-for="item in house_type_list" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-checkbox v-model="isZero" label="此渠道的出售礼品价格是否为0" border></el-checkbox>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="saveCanalClick()" size="small">保 存</el-button>
        </div>
      </el-dialog>
      <!-- 破损弹窗 -->
      <el-dialog :close-on-click-modal="false" @closed="closeDialog()" width="600px" :visible.sync="uploadDialog">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm" label-position="top">
          <el-form-item label="礼品入库时间" prop="in_time">
            <el-date-picker type="date" v-model="ruleForm.in_time" style="width: 30%;" value-format="yyyy-MM-dd" format="yyyy-MM-dd"></el-date-picker>
          </el-form-item>
          <el-form-item label="破损状况及原因" prop="break_reason">
            <el-input type="textarea" v-model="ruleForm.break_reason" :maxlength="999"></el-input>
          </el-form-item>
          <el-form-item label="上传破损处图片" ref="imgUpload" prop="imgList">
            <div class="img_up">
              <el-upload class="avatar-uploader" list-type="picture-card" ref="upload" action="123" :limit="5" :on-exceed="exceedFile" :multiple="multiple"
                :file-list="imgList" :before-remove="handleBeforeRemove" :on-change="uploadChange" :on-remove="handleRemove" :auto-upload="false"
                accept=".jpg, .jpeg, .png, .JPG, .JPEG">
                <i class="el-icon-plus"></i>
              </el-upload>
            </div>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" size="small" @click="saveDamaged('ruleForm')" :loading="loading">保 存</el-button>
        </div>
      </el-dialog>
    </div>
    <!-- 自定义底部 -->
    <div class="bottom_box">
      <el-pagination class="pull-left" background @current-change="handleCurrentChange" :current-page.sync="currentPage" :page-size="20"
        layout="prev, pager, next, jumper" :total="totalDataNumber"></el-pagination>
      <el-badge :value="car_gift_num" class="item pull-right">
        <el-button type="text" size="small" @click="goodscar(formInline.store_id,formInline.house_id,2)">
          <svg-icon style="font-size:18px" icon-class="car" />
        </el-button>&nbsp;
      </el-badge>
    </div>
  </div>
</template>

<script>
import {
  store,
  house,
  getList,
  getPage,
  addcar_submit,
  car,
  delGift,
  delCarList,
  save,
  get_house_type,
  saveCana,
  uplode_break_gift_img,
  del_gift_img,
  break_gift_img_list,
  check_img,
  upload_img
} from '@/api/storage_control/add_in_allocating.js';
import { getSts } from '@/api/feedback';
import axios from 'axios';
import { trim, pickerOptionsF } from '@/utils';
export default {
  data() {
    const uploadValidator = (rule, value, callback) => {
      if (this.imgList.length === 0) {
        callback(new Error('请上传图片'));
      } else {
        callback();
      }
    };
    return {
      formData: {},
      loading: false,
      is_store_main: '',
      multiple: true,
      ruleForm: {
        in_time: '',
        break_reason: ''
      },
      imgList: [],
      rules: {
        imgList: [{ validator: uploadValidator.bind(this) }],
        in_time: [{ required: true, message: '请选择日期' }],
        break_reason: [{ required: true, message: '请填写破损状况及原因' }]
      },
      uploadDialog: false,
      btnLoding: false,
      listLoading: false,
      currentPage: 1,
      totalDataNumber: 0,
      formInline: {
        store_id: '',
        house_id: '',
        gift_id: '',
        order_by: ''
      },
      isgiftDamaged: null,
      WarnisBlur: false,
      houseVal: '',
      in_type: 0,
      isDisable: false,
      store_list: null,
      house_list: [],
      house_type_list: [],
      list: null,
      show_carList: false,
      addCanalDialog: false,
      carList: [],
      car_gift_num: '',
      houseList: '',
      isZero: false,
      addCanalForm: {
        store_id: '',
        name: '',
        is_zero: '',
        is_gift_machine: ''
      },
      saveForm: {},
      totalOrder: '',
      totalPrice: ''
    };
  },
  beforeRouteEnter(to, from, next) {
    next(vm => {
      if (!vm.store_list) {
        vm.getstore();
        vm.getHouseType();
      }
    });
  },
  methods: {
    isBlur() {
      if (this.isDisable) {
        this.WarnisBlur = true;
      } else {
        this.WarnisBlur = false;
      }
    },
    // 上传图片改变
    uploadChange(file, fileList) {
      const isIMAGE = file.type === 'image/jpeg' || 'image/jpg' || 'image/png';
      const isLt1M = file.size / 1024 / 1024 < 2;
      this.$refs.ruleForm.clearValidate(['imgList']);
      if (!isIMAGE) {
        this.$message.error('上传图片格式只能为jpg、png、jpeg!');
        fileList.splice(fileList.length - 1, 1);
        return;
      }
      if (!isLt1M) {
        this.$message.error('上传文件大小不能超过 2MB!');
        fileList.splice(fileList.length - 1, 1);
        return;
      }
      this.imgList = fileList;
    },
    // 删除图片前执行
    handleBeforeRemove(file, fileList) {
      return this.$confirm('确定删除该图片？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.imgList = fileList;
        if (file.isOld) {
          del_gift_img({ id: file.id }).then(res => {
            if (res && res.code === 1) {
              this.$message.success(res.message || '删除成功');
            }
          });
        }
      });
    },
    // 删除图片
    handleRemove(file, fileList) {
      this.imgList = fileList;
    },
    // 上传图片数量限制
    exceedFile(files, fileList) {
      this.$message.error('最多只能上传5张图片');
    },
    // 保存
    saveDamaged(formName) {
      this.$refs[formName].validate(vaild => {
        if (vaild) {
          this.loading = true;
          this.uploadFile();
        }
      });
    },
    // 关闭弹窗
    closeDialog() {
      this.ruleForm = {
        in_time: '',
        break_reason: ''
      };
      this.imgList = [];
    },
    // 重置弹窗表单数据
    resetFrom() {
      this.ruleForm = {
        in_time: '',
        break_reason: ''
      };
      this.imgList = [];
      this.uploadDialog = false;
      this.goodscar(this.formInline.store_id, this.formInline.house_id, 2, this.in_type, this.houseVal);
    },
    //上传图片
    uploadFile() {
      const imgList = this.imgList;
      const { gift_id, uid, store_id, id } = this.formData;
      const { in_time, break_reason } = this.ruleForm;
      let folder_name = location.host == 'scfy.i3020.com' || location.host == 'ims.i3020.com' ? 'gift' : 'giftdev';
      check_img({ gift_id }).then(res => {
        if (res.code == 1) {
          getSts({}, 2).then(response => {
            let host = 'https://img.hahaipi.com';
            let policyText = {
              expiration: '2022-01-01T12:00:00.000Z', //设置该Policy的失效时间，超过这个失效时间之后，就没有办法通过这个policy上传文件了
              conditions: [
                ['content-length-range', 0, 1048576000] // 设置上传文件的大小限制
              ]
            };
            let upLoadData = {};
            let progress = 0;
            upLoadData.img_list = [];
            upLoadData.gift_id = gift_id;
            upLoadData.uid = uid;
            upLoadData.store_id = store_id;
            upLoadData.break_reason = break_reason;
            upLoadData.in_time = in_time;
            upLoadData.car_id = id;
            imgList.forEach((val, i) => {
              let imgSuffix = val.name.split('.');
              let suffix = imgSuffix[imgSuffix.length - 1];
              let policyBase64 = Base64.encode(JSON.stringify(policyText));
              let bytes = Crypto.HMAC(Crypto.SHA1, policyBase64, response.AccessKeySecret, { asBytes: true });
              let signature = Crypto.util.bytesToBase64(bytes);
              let config = {
                headers: {
                  'Content-Type': 'multipart/form-data'
                }
              };
              let date = new Date();
              let month = date.getMonth() < 9 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1;
              let day = date.getDate() < 9 ? '0' + date.getDate() : date.getDate();
              let h = date.getHours() < 9 ? '0' + date.getHours() : date.getHours();
              let min = date.getMinutes() < 9 ? '0' + date.getMinutes() : date.getMinutes();
              let s = date.getSeconds() < 9 ? '0' + date.getSeconds() : date.getSeconds();
              let dateYMD = `${date.getFullYear()}${month}${day}`;
              let dateHMS = `${h}${min}${s}`;
              let data = new FormData();
              let imgName = `${folder_name}/${this.formData.gift_id}/${dateYMD}${dateHMS}${this.S4(10000, 99999)}.${suffix}`;
              data.append('key', imgName);
              data.append('policy', policyBase64);
              data.append('OSSAccessKeyId', response.AccessKeyId);
              data.append('success_action_status', 200);
              data.append('x-oss-security-token', response.SecurityToken);
              data.append('signature', signature);
              data.append('file', val.raw);
              upLoadData.img_list.push({ img: imgName, gift_id: this.giftId });
              axios
                .post(host, data, config)
                .then(res => {
                  progress++;
                  if (progress == imgList.length) {
                    uplode_break_gift_img(upLoadData).then(res => {
                      if (res.code === 1) {
                        this.loading = false;
                        this.$message({
                          type: 'success',
                          message: res.message || '操作成功'
                        });
                        this.resetFrom();
                      }
                    });
                  }
                })
                .catch(res => {
                  this.$message({
                    type: 'error',
                    message: JSON.stringify(res)
                  });
                });
            });
          });
        } else {
          this.$message({ type: 'error', message: res.message });
        }
      });
    },
    S4(m, n) {
      return Math.floor(Math.random() * (m - n) + n);
    },
    uploading(row, storeId) {
      this.formData = row;
      this.ruleForm.in_time = row.in_time
        ? row.in_time
        : new Date(Date.UTC(new Date().getFullYear(), new Date().getMonth(), new Date().getDate())).toISOString().slice(0, 10);
      this.ruleForm.break_reason = row.break_reason;
      let data = {
        store_id: row.store_id,
        gift_id: row.gift_id,
        transfer_other_id: null
      };
      break_gift_img_list(data).then(res => {
        const imgList = (res && res.data) || [];
        this.imgList = imgList.map(item => {
          return {
            isOld: true,
            name: item.thumb,
            id: item.img_id,
            url: item.thumb
          };
        });
      });
      this.uploadDialog = true;
    },
    houseChange() {
      this.fetchData();
    },
    // 调入仓选择
    houseValChange(vId) {
      let obj = this.houseList.find(item => {
        return item.house_id === vId; //筛选出匹配数据
      });
      this.in_type = obj.is_store_main === '10' ? 1 : 0;
      this.is_store_main = obj.is_store_main;
      this.houseVal = vId;
      this.goodscar(this.formInline.store_id, this.formInline.house_id, 2, this.in_type, this.houseVal);
    },
    sortChange(column, prop, order) {
      this.listLoading = true;
      if (column.order == 'ascending') {
        if (column.column.label == '神采礼品编号') {
          this.formInline.order_by = 1;
        } else {
          this.formInline.order_by = 3;
        }
      } else if (column.order == 'descending') {
        if (column.column.label == '神采礼品编号') {
          this.formInline.order_by = 2;
        } else {
          this.formInline.order_by = 4;
        }
      } else {
        this.formInline.order_by = '';
      }
      this.fetchData();
    },
    getHouseType() {
      get_house_type({ category_id: 99 }).then(response => {
        this.house_type_list = response;
      });
    },
    getstore() {
      store({ category_id: 99 }).then(response => {
        this.store_list = response;
        let data = response.filter(res => res.id == this.$store.getters.optionId);
        if (data.length > 0) {
          this.formInline.store_id = data[0].id;
        } else {
          if (this.store_list.length > 0) {
            this.formInline.store_id = this.store_list[0].id;
          }
        }
        this.gethouse(this.formInline.store_id);
      });
    },
    gethouse(id) {
      this.formInline.house_id = '';
      house(id).then(response => {
        this.house_list = response;
        if (this.house_list.length > 0) {
          this.formInline.house_id = this.house_list[0].house_id;
        }
        this.fetchData();
      });
    },
    // 搜索
    searchClick() {
      if (this.formInline.store_id) {
        let gift_id = this.formInline.gift_id;
        let reg = /，/g;
        gift_id = gift_id.replace(reg, ',');
        if (gift_id.charAt(gift_id.length - 1) == ',') {
          gift_id = gift_id.substr(0, gift_id.length - 1);
        }
        if (gift_id.split(',').length - 1 > 99) {
          this.$message({ type: 'error', message: '一次性最多查询100个' });
          return;
        }
        this.formInline.gift_id = trim(gift_id);
        this.fetchData();
      } else {
        this.$message({ type: 'error', message: '请选择门店' });
      }
    },
    goodscar(store_id, house_id, type, in_type = 0, houseVal = '') {
      this.listLoading = true;
      car({ store_id: store_id, house_id: house_id, type: type, in_type })
        .then(response => {
          response.forEach(function(val, i) {
            val.money_cost = ((val.money_cost * 1) / 1000000).toFixed(2);
          });
          this.carList = response;
          let isgiftDamaged =
            this.carList &&
            this.carList.find(item => {
              return item.in_time != '' || item.break_reason;
            });
          if (isgiftDamaged) {
            this.isDisable = true;
          } else {
            this.isDisable = false;
            this.WarnisBlur = false;
          }
          this.listLoading = false;
          this.show_carList = true;
          this.count();
          house(this.formInline.store_id).then(response => {
            this.houseList = response;
            if (isgiftDamaged) {
              let obj = this.houseList.find(item => {
                return item.is_store_main === '10';
              });
              this.houseVal = obj.house_id;
              this.is_store_main = '10';
            }
          });
        })
        .catch(res => {
          this.listLoading = false;
        });
    },
    // 新增渠道
    addCanalClick() {
      this.addCanalDialog = true;
      this.addCanalForm.is_gift_machine = '';
    },
    // 新增渠道提交
    saveCanalClick() {
      if (!this.addCanalForm.is_gift_machine) {
        this.$message({
          type: 'error',
          message: '选择店内仓'
        });
        return;
      }
      this.addCanalForm.is_zero = this.isZero ? 1 : 0;
      this.addCanalForm.store_id = this.formInline.store_id;
      saveCana(this.addCanalForm).then(response => {
        this.$message({
          type: response.code == 1 ? 'success' : 'error',
          message: response.message
        });
        if (response.code == 1) {
          this.addCanalDialog = false;
          this.gethouse(this.formInline.store_id);
        }
      });
    },
    //确认调拨
    saveClick() {
      if (this.carList.length == 0) {
        this.$message({ type: 'error', message: '大佬，你购物车空空如也' });
        return;
      }
      if (this.is_store_main == '10') {
        let carMes =
          this.carList &&
          this.carList.find(val => {
            return val.in_time == '' || val.break_img.ratio == 100 || val.break_reason == null;
          });
        if (carMes) {
          this.$message({ type: 'error', message: '部分礼品未上传破损信息，请完善信息后再调拨' });
          return;
        }
      }
      let err = false;
      this.saveForm = {
        store_id: this.formInline.store_id,
        type: 1,
        house_in: this.houseVal,
        house_out: this.formInline.house_id,
        gift: [],
        in_type: this.is_store_main == '10' ? 1 : 0
      };
      if (!this.houseVal) {
        this.$message({ type: 'error', message: '请选择调入仓' });
        return;
      }
      if (this.houseVal == this.formInline.house_id) {
        this.$message({ type: 'error', message: '请选择正确的调入仓' });
        return;
      }
      for (var k = 0; k < this.carList.length; k++) {
        if (this.carList[k].gift_num * 1 > this.carList[k].num * 1) {
          err = true;
          break;
        }
      }
      if (err) {
        this.$message({ type: 'error', message: '调拨数不可大于当前库存数' });
        return;
      }
      for (let i = 0; i < this.carList.length; i++) {
        this.saveForm.gift.push({
          gift_id: this.carList[i].gift_id,
          num: this.carList[i].gift_num,
          look_money: this.carList[i].money_cost * 1000000,
          money_cost: this.carList[i].money_cost * 1000000,
          break_reason: this.carList[i].break_reason,
          in_time: this.carList[i].in_time
        });
      }
      var data = this.saveForm;
      this.btnLoding = true;
      save(data)
        .then(response => {
          this.$message({
            type: response.code == 1 ? 'success' : 'error',
            message: response.message
          });
          if (response.code == 1) {
            this.btnLoding = false;
            this.show_carList = false;
          }
        })
        .catch(() => {
          this.btnLoding = false;
        });
    },
    // 计算购物车
    count() {
      var totalPrice = 0; //临时总价
      var totalOrder = 0;
      this.carList.forEach(function(val, index) {
        totalPrice += val.gift_num * 1 * val.money_cost * 1; //累计总价
        totalOrder += val.gift_num * 1;
      });
      this.totalPrice = parseFloat(totalPrice);
      this.totalOrder = parseFloat(totalOrder);
    },
    // 加入购物车
    addcar(gift_id, num, store_id, house_id, index, allNum) {
      num *= 1;
      allNum *= 1;
      var data = {
        gift_id: gift_id,
        type: 2,
        num: allNum <= 0 ? 0 : num <= 0 ? 1 : num > allNum ? allNum : num,
        store_id: store_id,
        house_id: house_id
      };
      this.listLoading = true;
      addcar_submit(data).then(response => {
        this.listLoading = false;
        if (response.code == 1) {
          this.list[index].in_car = 1;
          this.car_gift_num += 1;
          this.$message({
            type: 'success',
            message: '添加成功'
          });
        }
      });
    },
    // 清空购物车
    delCarListClick(store_id, house_id) {
      delCarList({ store_id: store_id, house_id: house_id, type: 2, in_type: 1 }).then(response => {
        this.goodscar(this.formInline.store_id, this.formInline.house_id, 2, this.in_type, this.houseVal);
      });
    },
    // 移除
    delClick(gift_id, store_id, house_id, index, in_time) {
      delGift({
        gift_id: gift_id,
        house_id: house_id,
        store_id: store_id,
        type: 2,
        in_type: in_time ? 1 : 0
      }).then(response => {
        this.$message({
          type: response.code == 1 ? 'success' : 'error',
          message: response.message
        });
        if (response.code == 1) {
          this.goodscar(this.formInline.store_id, this.formInline.house_id, 2, this.in_type, this.houseVal);
        }
      });
    },
    car_close() {
      this.handleCurrentChange(this.currentPage);
    },
    // 列表获取
    fetchData() {
      this.listLoading = true;
      this.currentPage = 1;
      getPage(this.formInline).then(response => {
        this.totalDataNumber = response * 20;
        getList(this.formInline).then(response => {
          this.car_gift_num = response.length > 0 ? response[0].car_gift_num : 0;
          this.list = response;
          this.listLoading = false;
        });
      });
    },
    // 分页
    handleCurrentChange(val) {
      this.listLoading = true;
      getList(this.formInline, val).then(response => {
        if (response.length > 0) {
          this.car_gift_num = response[0].car_gift_num;
        }
        this.list = response;
        this.listLoading = false;
      });
    }
  }
};
</script>

<style  lang="scss" scoped>
.el-form-item /deep/ .el-upload--picture-card {
  width: 80px;
  height: 80px;
}

.el-form-item /deep/ .el-upload {
  width: 80px;
  height: 80px;
  line-height: 80px;
}

.el-form-item /deep/ .el-upload-list--picture-card .el-upload-list__item {
  width: 80px;
  height: 80px;
  transition: none !important;
}
</style>